﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Net;
using System.Xml;
using System.Runtime.InteropServices;


namespace _91cc.Main
{
    public partial class Main : DevExpress.XtraEditors.XtraForm
    {
        NotifyIcon m_NotifyIcon;

        public Main()
        {
            InitializeComponent();

            this.defaultLookAndFeel1.LookAndFeel.SkinName = "Office 2007 Blue";

            SetNotify();
            SetLocation();
            InitRSS();
        }

        private void SetNotify()
        {
            m_NotifyIcon = new NotifyIcon();
            m_NotifyIcon.Text = "个人超级桌面!";
            m_NotifyIcon.Icon = MyResource.favicon;
            m_NotifyIcon.MouseDoubleClick += new MouseEventHandler(notifyIcon1_MouseDoubleClick);
            m_NotifyIcon.Visible = true;
            m_NotifyIcon.ContextMenuStrip = this.contextMenuStrip1;
        }
        public void SetLocation()
        {
            int x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
            int y = Screen.PrimaryScreen.WorkingArea.Height - this.Height - 5;
            this.Location = new Point(x, y);
        }
        public void InitRSS()
        {
            popupMenu1.ClearLinks();
            foreach (Config.RssBase item in Config.RSSConfig.Instance.RssBases)
            {
                DevExpress.XtraBars.BarButtonItem barItem = new DevExpress.XtraBars.BarButtonItem();
                barItem.Caption = item.Caption;
                barItem.Tag = item.Link;
                barItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barItem_ItemClick);
                popupMenu1.AddItem(barItem);
            }
        }
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Activate();
            this.Show();
        }



        private void Main_Load(object sender, EventArgs e)
        {
            if (popupMenu1.ItemLinks.Count > 0)
            {
                popupMenu1.ItemLinks[0].Item.PerformClick();
            }
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void BindData(string url)
        {
            DevExpress.Utils.WaitDialogForm dlg = new DevExpress.Utils.WaitDialogForm("查询数据中,请稍后...", "Power Design By Ft_CaiCai", new Size(240, 40), this);
            try
            {
                dlg.Show();
                XmlDocument m_XmlDocument = RssDoc.GetDoc(url, 3, false);
                RssDoc m_RssDoc = new RssDoc(m_XmlDocument);

                gridControl1.DataSource = m_RssDoc.List;
            }
            catch
            {
                dlg.Close();
            }
            finally
            {
                dlg.Close();
                dlg.Dispose();
            }
        }

        private void gridControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            GC_ClickEvent(false);
        }
        private void gridControl1_MouseClick(object sender, MouseEventArgs e)
        {
            GC_ClickEvent(true);
        }
        private void GC_ClickEvent(bool singClick)
        {
            if (singClick == Config.RSSConfig.Instance.OnClick)
            {
                string tempUrl = gridView1.GetFocusedRowCellDisplayText("Link");
                ShowIEByUrl(tempUrl);
            }
        }


        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.m_NotifyIcon.Dispose();
        }

        private void Main_Activated(object sender, EventArgs e)
        {
            txt_select.Focus();
        }

        private void btn_baidu_Click(object sender, EventArgs e)
        {
            string strSelect = txt_select.Text.Trim();
            if (!string.IsNullOrEmpty(strSelect))
            {
                txt_select.Text = string.Empty;
                string strUrl = "http://www.baidu.com/s?wd=" + strSelect;
                ShowIEByUrl(strUrl);
            }
        }
        public void ShowIEByUrl(string strUrl)
        {
            System.Diagnostics.Process.Start(strUrl);
        }

        

        void barItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ddbtn.Tag = e.Item.Tag.ToString();
            ddbtn.Text = e.Item.Caption;
            BindData(e.Item.Tag.ToString());
        }

        private void ddbtn_DoubleClick(object sender, EventArgs e)
        {
            string url = ddbtn.Tag.ToString();
            if (!string.IsNullOrEmpty(url))
            {
                BindData(ddbtn.Tag.ToString());
            }
        }

        private void btn_hide_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        private void txt_select_EditValueChanged(object sender, EventArgs e)
        {
            string selValue = txt_select.Text.Trim();

            if (string.IsNullOrEmpty(selValue))
            {

            }
            else
            {

            }
        }

        private void menu_set_Click(object sender, EventArgs e)
        {
            URLSet frm = new URLSet();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                InitRSS();
            }
        }

        private void menu_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}